from flask import Blueprint, request

from packages.core.auth.login_user_holder import LoginUserHolder
from packages.common.base import R
from packages.core.auth.decorators import HasPerm
from packages.common.base_model import db
from modules.sys.services.menu_service import MenuService
from packages.common.base import IdForm, IdsForm
from modules.sys.validators.menu_validator import MenuForm, MenuPageForm

# 声明一个蓝图
menu = Blueprint("menu", __name__, url_prefix="")

# 声明一个菜单业务服务
menu_service = MenuService(db=db)


@menu.route("/sys/menu/detail", methods=["POST"])
@HasPerm(access="sys:menu:detail", name="通过id获取菜单信息")
def menu_detail():
    """
    通过id获取菜单信息
    :return:
    """
    form = IdForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # 或者直接拿id值 id=form.id.data
    # u = Menu.query.filter_by(id=form.id.data).first()
    # 通过主键查询
    u = menu_service.detail(form)
    if u is not None:
        return R.data(u.to_dict())
    else:
        return R.fail("该记录不存在")


@menu.route("/sys/menu/page", methods=["POST"])
@HasPerm(access="sys:menu:page", name="分页查询菜单列表")
def menu_page():
    """
    分页查询菜单列表
    :return:
    """
    form = MenuPageForm()
    form.validate_for_api()
    return R.data(menu_service.page(form))


@menu.route("/sys/menu/list", methods=["POST"])
@HasPerm(access="sys:menu:list", name="查询菜单列表")
def menu_list():
    """
    查询菜单列表
    :return:
    """
    return R.data(menu_service.list())


@menu.route("/sys/menu/save", methods=["POST"])
@HasPerm(access="sys:menu:save", name="添加菜单")
def menu_save():
    """
    添加菜单
    :return:
    """
    form = MenuForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    menu_service.save(form)
    return R.success("添加菜单成功")


@menu.route("/sys/menu/update", methods=["POST"])
@HasPerm(access="sys:menu:update", name="修改菜单")
def menu_update():
    """
    修改菜单
    :return:
    """
    form = MenuForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    menu_service.update(form)
    return R.success("修改菜单成功")


@menu.route("/sys/menu/remove", methods=["POST"])
@HasPerm(access="sys:menu:remove", name="删除菜单")
def menu_remove():
    """
    删除菜单
    :return:
    """
    form = IdsForm()
    form.validate_for_api()
    # 可通过form.data获取所有提交参数
    # print(form.data)
    menu_service.remove(form)
    return R.success("删除菜单成功")


@menu.route("/sys/menu/appList", methods=["POST"])
@HasPerm(name="应用列表")
def menu_app_list():
    """
    应用列表
    :return:
    """
    return R.data(menu_service.app_list())


@menu.route("/sys/menu/tree", methods=["POST"])
@HasPerm(access="sys:menu:tree", name="查询菜单树")
def menu_tree():
    """
    查询菜单树
    :return:
    """
    return R.data(menu_service.tree())


@menu.route("/sys/menu/syncRoute", methods=["POST"])
@HasPerm(access="sys:menu:syncRoute", name="同步前端路由")
def sync_route():
    """
    同步前端路由
    :return:
    """
    json = request.json
    menu_service.sync_route(json)
    return R.success()


@menu.route("/menu/all", methods=["GET"])
def user_route_menu_v5():
    """
    获取用户路由菜单-v5版
    :return:
    """

    return R.data(menu_service.user_route_menu("v5"))


@menu.route("/getMenuList", methods=["GET"])
def user_route_menu_v2():
    """
    获取用户路由菜单-v2版
    :return:
    """

    return R.data(menu_service.user_route_menu("v2"))


@menu.route("/getArtDesignMenu", methods=["GET"])
def user_route_menu_art_design_pro():
    """
    获取用户路由菜单-art-design-pro版
    :return:
    """

    return R.data(menu_service.user_route_menu("art-design-pro"))


@menu.route("/badgeConfig", methods=["POST"])
def user_badge_config():
    """
    获取菜单徽标配置
    :return:
    """
    current_user = LoginUserHolder.get()
    user_id = current_user.get("id")
    return R.data(menu_service.user_badge_config(user_id))
